package com.ruoyi.drone.service.impl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.drone.mapper.DroneTaskApplicationMapper;
import com.ruoyi.drone.domain.DroneTaskApplication;
import com.ruoyi.drone.service.IDroneTaskApplicationService;

/**
 * 任务接单Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-01-01
 */
@Service
public class DroneTaskApplicationServiceImpl implements IDroneTaskApplicationService 
{
    @Autowired
    private DroneTaskApplicationMapper droneTaskApplicationMapper;

    /**
     * 查询任务接单
     * 
     * @param applicationId 任务接单主键
     * @return 任务接单
     */
    @Override
    public DroneTaskApplication selectDroneTaskApplicationByApplicationId(Long applicationId)
    {
        return droneTaskApplicationMapper.selectDroneTaskApplicationByApplicationId(applicationId);
    }

    /**
     * 查询任务接单列表
     * 
     * @param droneTaskApplication 任务接单
     * @return 任务接单
     */
    @Override
    public List<DroneTaskApplication> selectDroneTaskApplicationList(DroneTaskApplication droneTaskApplication)
    {
        return droneTaskApplicationMapper.selectDroneTaskApplicationList(droneTaskApplication);
    }

    /**
     * 根据任务ID查询任务接单列表
     * 
     * @param taskId 任务ID
     * @return 任务接单集合
     */
    @Override
    public List<DroneTaskApplication> selectDroneTaskApplicationListByTaskId(Long taskId)
    {
        return droneTaskApplicationMapper.selectDroneTaskApplicationListByTaskId(taskId);
    }

    /**
     * 根据飞手ID查询任务接单列表
     * 
     * @param pilotId 飞手ID
     * @return 任务接单集合
     */
    @Override
    public List<DroneTaskApplication> selectDroneTaskApplicationListByPilotId(Long pilotId)
    {
        return droneTaskApplicationMapper.selectDroneTaskApplicationListByPilotId(pilotId);
    }

    /**
     * 根据任务ID和飞手ID查询任务接单
     * 
     * @param taskId 任务ID
     * @param pilotId 飞手ID
     * @return 任务接单
     */
    @Override
    public DroneTaskApplication selectDroneTaskApplicationByTaskIdAndPilotId(Long taskId, Long pilotId)
    {
        return droneTaskApplicationMapper.selectDroneTaskApplicationByTaskIdAndPilotId(taskId, pilotId);
    }

    /**
     * 新增任务接单
     * 
     * @param droneTaskApplication 任务接单
     * @return 结果
     */
    @Override
    public int insertDroneTaskApplication(DroneTaskApplication droneTaskApplication)
    {
        droneTaskApplication.setCreateTime(DateUtils.getNowDate());
        droneTaskApplication.setApplyTime(DateUtils.getNowDate());
        droneTaskApplication.setStatus("0"); // 默认状态为待审核
        return droneTaskApplicationMapper.insertDroneTaskApplication(droneTaskApplication);
    }

    /**
     * 修改任务接单
     * 
     * @param droneTaskApplication 任务接单
     * @return 结果
     */
    @Override
    public int updateDroneTaskApplication(DroneTaskApplication droneTaskApplication)
    {
        droneTaskApplication.setUpdateTime(DateUtils.getNowDate());
        return droneTaskApplicationMapper.updateDroneTaskApplication(droneTaskApplication);
    }

    /**
     * 批量删除任务接单
     * 
     * @param applicationIds 需要删除的任务接单主键
     * @return 结果
     */
    @Override
    public int deleteDroneTaskApplicationByApplicationIds(Long[] applicationIds)
    {
        return droneTaskApplicationMapper.deleteDroneTaskApplicationByApplicationIds(applicationIds);
    }

    /**
     * 删除任务接单信息
     * 
     * @param applicationId 任务接单主键
     * @return 结果
     */
    @Override
    public int deleteDroneTaskApplicationByApplicationId(Long applicationId)
    {
        return droneTaskApplicationMapper.deleteDroneTaskApplicationByApplicationId(applicationId);
    }

    /**
     * 检查任务和飞手的接单记录是否唯一
     * 
     * @param taskId 任务ID
     * @param pilotId 飞手ID
     * @return 结果
     */
    @Override
    public boolean checkTaskApplicationUnique(Long taskId, Long pilotId)
    {
        int count = droneTaskApplicationMapper.checkTaskApplicationUnique(taskId, pilotId);
        return count == 0;
    }

    /**
     * 更新任务接单状态
     * 
     * @param droneTaskApplication 任务接单
     * @return 结果
     */
    @Override
    public int updateDroneTaskApplicationStatus(DroneTaskApplication droneTaskApplication)
    {
        droneTaskApplication.setUpdateTime(DateUtils.getNowDate());
        return droneTaskApplicationMapper.updateDroneTaskApplicationStatus(droneTaskApplication);
    }

    /**
     * 审核任务接单
     * 
     * @param droneTaskApplication 任务接单
     * @return 结果
     */
    @Override
    public int auditDroneTaskApplication(DroneTaskApplication droneTaskApplication)
    {
        droneTaskApplication.setAuditTime(DateUtils.getNowDate());
        droneTaskApplication.setUpdateTime(DateUtils.getNowDate());
        return droneTaskApplicationMapper.auditDroneTaskApplication(droneTaskApplication);
    }

    /**
     * 飞手申请任务
     * 
     * @param droneTaskApplication 任务接单
     * @return 结果
     */
    @Override
    public int applyTask(DroneTaskApplication droneTaskApplication)
    {
        // 检查是否已经申请过该任务
        if (!checkTaskApplicationUnique(droneTaskApplication.getTaskId(), droneTaskApplication.getPilotId())) {
            throw new RuntimeException("您已经申请过该任务，不能重复申请");
        }
        
        droneTaskApplication.setCreateBy(SecurityUtils.getUsername());
        droneTaskApplication.setCreateTime(DateUtils.getNowDate());
        droneTaskApplication.setApplyTime(DateUtils.getNowDate());
        droneTaskApplication.setStatus("0"); // 待审核状态
        
        return droneTaskApplicationMapper.insertDroneTaskApplication(droneTaskApplication);
    }

    /**
     * 通过任务申请
     * 
     * @param applicationId 申请ID
     * @param auditBy 审核人
     * @return 结果
     */
    @Override
    public int approveApplication(Long applicationId, String auditBy)
    {
        DroneTaskApplication application = new DroneTaskApplication();
        application.setApplicationId(applicationId);
        application.setStatus("1"); // 已通过
        application.setAuditBy(auditBy);
        application.setAuditTime(DateUtils.getNowDate());
        application.setUpdateBy(auditBy);
        application.setUpdateTime(DateUtils.getNowDate());
        
        return droneTaskApplicationMapper.auditDroneTaskApplication(application);
    }

    /**
     * 拒绝任务申请
     * 
     * @param applicationId 申请ID
     * @param rejectReason 拒绝原因
     * @param auditBy 审核人
     * @return 结果
     */
    @Override
    public int rejectApplication(Long applicationId, String rejectReason, String auditBy)
    {
        DroneTaskApplication application = new DroneTaskApplication();
        application.setApplicationId(applicationId);
        application.setStatus("2"); // 已拒绝
        application.setRejectReason(rejectReason);
        application.setAuditBy(auditBy);
        application.setAuditTime(DateUtils.getNowDate());
        application.setUpdateBy(auditBy);
        application.setUpdateTime(DateUtils.getNowDate());
        
        return droneTaskApplicationMapper.auditDroneTaskApplication(application);
    }

    /**
     * 取消任务申请
     * 
     * @param applicationId 申请ID
     * @return 结果
     */
    @Override
    public int cancelApplication(Long applicationId)
    {
        DroneTaskApplication application = new DroneTaskApplication();
        application.setApplicationId(applicationId);
        application.setStatus("3"); // 已取消
        application.setUpdateBy(SecurityUtils.getUsername());
        application.setUpdateTime(DateUtils.getNowDate());
        
        return droneTaskApplicationMapper.updateDroneTaskApplicationStatus(application);
    }
}
